import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { reactive } from 'vue';

import { renderFilterHeader } from '#/utils/grid-filter';

export const filterData = reactive<Record<string, any>>({});
export const sortData = reactive<Record<string, any>>({});
// 添加全局gridApi引用
let gridApiRef: any = null;

export function setGridApi(api: any) {
  gridApiRef = api;
}
// 分页参数
const pageVO = reactive({
  currentPage: 1,
  pageSize: 20,
  totalRow: 0,
});
const handleSearch = () => {
  pageVO.currentPage = 1; // 重置到第一页
  gridApiRef.query();
};

export function useColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'id',
      title: 'ID',
      width: 50,
      slots: {
        header: ({ column }) =>
          renderFilterHeader(
            { column },
            {
              filterData, // 响应式数据脱敏
              sortData,
              url: '/admin/account/searchByHeard',
              onFilterChange: handleSearch,
            },
          ),
      },
    },
    {
      field: 'realname',
      title: '昵称',
      width: 250,
      slots: {
        header: ({ column }) =>
          renderFilterHeader(
            { column },
            {
              filterData, // 响应式数据脱敏
              sortData,
              url: '/admin/account/searchByHeard',
              onFilterChange: handleSearch,
            },
          ),
      },
    },
    {
      field: 'username',
      title: '用户名',
      width: 250,
      slots: {
        header: ({ column }) =>
          renderFilterHeader(
            { column },
            {
              filterData, // 响应式数据脱敏
              sortData,
              url: '/admin/account/searchByHeard',
              onFilterChange: handleSearch,
            },
          ),
      },
    },
    {
      field: 'is_active_str',
      title: '状态',
      width: 150,
      slots: {
        header: ({ column }) =>
          renderFilterHeader(
            { column },
            {
              filterData, // 响应式数据脱敏
              sortData,
              url: '/admin/account/searchByHeard',
              onFilterChange: handleSearch,
            },
          ),
      },
    },
  ];
}
